package org.example.controller;


import org.example.pojo.Result;
import org.example.util.AliyunOSSOperator;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

//上传控制器
@RestController
public class UploadController {

    //帮我生成日志
    private static final Logger log = LoggerFactory.getLogger(UploadController.class);
    // 获取阿里云工具对象
    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;

    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        if (!file.isEmpty()) {
            // 获取原始文件名后缀
            String originalFilename = file.getOriginalFilename();
            // 获取文件字节数组
            byte[] content = file.getBytes();
            //调用 upload方法 将文件上传到阿里云服务器
            String url = aliyunOSSOperator.upload(content, originalFilename);

            return Result.success(url);
        }

        return Result.error("文件有误,上传失败");
    }
}
